package rps.client.ui;

import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JFrame;
import javax.swing.SwingConstants;

import rps.client.UIController;
import rps.game.Game;
import rps.game.data.FigureKind;
import rps.game.data.Player;


public class Choices {
	
	private JButton paperButton, rockButton, scissorsButton;
	private UIController uiController;
	private Player player;
	private Game game;
	private boolean initial;
	
	private JFrame choiceWindow;
	private JFrame endWindow;
	
	private JLabel choiceLabel;
	private JLabel message;
	
	//----------------------------- Images --------------------------------------------------
		private final String projPath = System.getProperty("user.dir");
		private final String background = "/src/rps/client/ui/images/background/";
		private final String opponent = "/src/rps/client/ui/images/opponent/";
		private final String own = "/src/rps/client/ui/images/own/";
		
		private final String flag = "flag/";
		private final String trap = "trap/";
		private final String scissors = "scissors/";
		private final String rock = "rock/";
		private final String paper = "paper/";
		private final String hidden = "hidden/";
		
		//own figures:
		public final  String ownNormalFlag = projPath + own + flag + "normal.png";
		public final  String ownSelectedFlag = projPath + own + flag + "selected.png";
		public final  String ownAssignFlag = projPath + own + flag + "assign.png";
		
		public final  String ownNormalRock = projPath + own + rock + "normal.png";
		public final  String ownSelectedRock = projPath + own + rock + "selected.png";
		public final  String ownNormalHiddenRock = projPath + own + rock + hidden + "normal.png";
		public final  String ownSelectedHiddenRock = projPath + own + rock + hidden + "selected.png"; 

		public final  String ownNormalTrap = projPath + own + trap + "normal.png";
		public final  String ownSelectedTrap = projPath + own + trap + "selected.png";
		public final  String ownAssignTrap = projPath + own + trap + "assign.png";

		public final  String ownNormalPaper = projPath + own + paper + "normal.png";
		public final  String ownSelectedPaper = projPath + own + paper + "selected.png";
		public final  String ownNormalHiddenPaper = projPath + own + paper + hidden + "normal.png";
		public final  String ownSelectedHiddenPaper = projPath + own + paper + hidden + "selected.png"; 

		public final  String ownNormalScissors = projPath + own + scissors + "normal.png";
		public final  String ownSelectedScissors = projPath + own + scissors + "selected.png";
		public final  String ownNormalHiddenScissors = projPath + own + scissors + hidden + "normal.png";
		public final  String ownSelectedHiddenScissors = projPath + own + scissors + hidden + "selected.png"; 
		
		//opponent figures:
		public final  String opponentNormalFlag = projPath + opponent + flag + "normal.png";
		
		public final  String opponentNormalRock = projPath + opponent + rock + "normal.png";
		public final  String opponentAttackRock = projPath + opponent + rock + "possibleAttack.png";

		public final  String opponentNormalTrap = projPath + opponent + trap + "normal.png";

		public final  String opponentNormalPaper = projPath + opponent + paper + "normal.png";
		public final  String opponentAttackPaper = projPath + opponent + paper + "possibleAttack.png";

		public final  String opponentNormalScissors = projPath + opponent + scissors + "normal.png";
		public final  String opponentAttackScissors = projPath + opponent + scissors + "possibleAttack.png";
		
		public final String opponentNormalHidden = projPath + opponent + hidden + "normal.png";
		public final String opponentAttackHidden = projPath + opponent + hidden + "possibleAttack.png";
		
		//background images:
		public final  String normalBackground = projPath  + background + "normal.png";
		public final  String moveBackground = projPath  + background + "move.png";
		//---------------------------------------------------------------------------------------

	
	public Choices(Player player, Game game, UIController uiController){
		super();
		this.player = player;
		this.game = game;
		this.uiController = uiController;
		
		//constructs endWindow
		endWindow = new JFrame();
		endWindow.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		
		JSplitPane end;
		
		JPanel messagePanel = new JPanel();
		
		message = new JLabel(" ", SwingConstants.CENTER);
		messagePanel.add(message);
		
		
		
		JPanel buttons = new JPanel();
		buttons.setLayout(new BoxLayout(buttons, BoxLayout.X_AXIS));
		
		JButton newGameButton, exitButton;
		
		newGameButton = new JButton("New Game!");
		newGameButton.addActionListener(new NewGameButtonListener());
		
		exitButton = new JButton("Exit");
		exitButton.addActionListener(new ExitButtonListener());
		
		
		buttons.add(newGameButton);
		buttons.add(exitButton);
		
		
		end = new JSplitPane(JSplitPane.VERTICAL_SPLIT, true, messagePanel, buttons);
		end.setDividerSize(0);
		
		endWindow.add(end);
		endWindow.setLocationRelativeTo(null);
		
		hide(endWindow);
		//----------------------------------------------------------------
		
		
		//constructs choiceWindow
		choiceWindow = new JFrame();
		choiceWindow.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		choiceWindow.setResizable(false);
		
		JSplitPane choice;
		
		JPanel kinds = new JPanel();
		kinds.setLayout(new BoxLayout(kinds, BoxLayout.X_AXIS));
		
		JPanel label = new JPanel();
		

		choiceLabel = new JLabel(" ", SwingConstants.CENTER);
		label.add(choiceLabel);
		
		paperButton = new JButton();
		ImageIcon paperImage = new ImageIcon(ownNormalPaper);
		paperButton.setIcon(paperImage);
		paperButton.addActionListener(new PaperButtonListener());
		
		rockButton = new JButton();
		ImageIcon rockImage = new ImageIcon(ownNormalRock);
		rockButton.setIcon(rockImage);
		rockButton.addActionListener(new RockButtonListener());
		
		scissorsButton = new JButton();
		ImageIcon scissorsImage = new ImageIcon(ownNormalScissors);
		scissorsButton.setIcon(scissorsImage);
		scissorsButton.addActionListener(new ScissorsButtonListener());
		
		
		kinds.add(paperButton);
		kinds.add(rockButton);
		kinds.add(scissorsButton);
		
		
		choice = new JSplitPane(JSplitPane.VERTICAL_SPLIT, true, label, kinds);
		choice.setDividerSize(0);
		
		choiceWindow.add(choice);
		choiceWindow.setLocationRelativeTo(null);
		hide(choiceWindow);
		//-----------------------------------------------------------
	}
	
	public void won() {
		buildEndWindow("Congratulations! You've won the Game!");
		endWindow.setTitle("Congratulations!");
		endWindow.setVisible(true);
	}
	
	
	public void lost() {
		buildEndWindow("Soooo Bad. You've lost the Game...");
		endWindow.setTitle("Oh no...");
		endWindow.setVisible(true);
	}
	
	
	public void drawn() {
		buildEndWindow("Oh damn, a Draw... Try it again!");
		endWindow.setTitle("Keep it up!");
		endWindow.setVisible(true);
	}
	
	
	/**
	 * builds a window to display the result of the game
	 * @param text is shown depending on result
	 */
	private void buildEndWindow(String text){
		message.setText(text);
		show(endWindow);
	}
	
	
	/**
	 * constructs a Window where you can choose a kind
	 * @param b indicates whether it is an initial choice or a choice after a draw
	 */
	public void choice(boolean b) {
		this.initial = b;
		
		if (initial){
			choiceLabel.setText("Please provide your initial choice!");
			choiceWindow.setTitle("Which player begins?");
		}
		else {
			choiceLabel.setText("Please provide your choice for a Rebattle!");
			choiceWindow.setTitle("Battle decision");
		}
		
		
		show(choiceWindow);
	}
	
	
	private void hide(JFrame window) {
		window.setVisible(false);
		window.setVisible(false);
	}
	
	
	private void show(JFrame window) {
		window.revalidate();
		window.repaint();
		window.pack();
		
		Point center = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
		int width = window.getSize().width;
		int height = window.getSize().height;
		window.setBounds(center.x - width / 2, center.y - height / 2, width, height);
		
		window.setVisible(true);
	}
	
	
	private class NewGameButtonListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			uiController.handleNewGame();
			hide(endWindow);
			
		}
		
	}
	
	private class ExitButtonListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			uiController.handleExit();
		}
		
	}
	
	private class PaperButtonListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			try {
				if (initial)
					game.setInitialChoice(player, FigureKind.ROCK);
				else
					game.setUpdatedKindAfterDraw(player, FigureKind.PAPER);
				hide(choiceWindow);
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
	private class RockButtonListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			try {
				if (initial)
					game.setInitialChoice(player, FigureKind.ROCK);
				else
					game.setUpdatedKindAfterDraw(player, FigureKind.ROCK);
				hide(choiceWindow);
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		}
	}
	
	private class ScissorsButtonListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			try {
				if (initial)
					game.setInitialChoice(player, FigureKind.SCISSORS);
				else
					game.setUpdatedKindAfterDraw(player, FigureKind.SCISSORS);
				hide(choiceWindow);
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
}